package com.mkl.business.controller;

import com.mkl.business.domain.Inport;
import com.mkl.business.service.InportService;
import com.mkl.business.vo.InportVo;
import com.mkl.system.common.ActiveUser;
import com.mkl.system.common.ResultObj;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 13:13
 */
@RestController
@RequestMapping("api/inport")
public class InportController {

    @Autowired
    private InportService inportService;


    /**
     * 加载全部商品进货
     *
     * @param inportVo 进货
     * @return 返回结果
     */
    @RequestMapping("loadAllInport")
    public Object loadAllInport(InportVo inportVo) {
        return inportService.queryAllInport(inportVo);
    }

    /**
     * 新增商品进货
     *
     * @param inport 进货
     * @return 返回结果
     */
    @RequestMapping("addInport")
    public ResultObj addInport(Inport inport) {
        try {
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            inport.setOperateperson(activeUser.getUser().getName());
            inport.setInporttime(new Date());
            inportService.saveInport(inport);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 修改进货
     *
     * @param inport 进货
     * @return 返回结果
     */
    @RequestMapping("updateInport")
    public ResultObj updateInport(Inport inport) {
        try {
            inportService.updateInport(inport);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 根据id删除进货
     *
     * @param id id
     * @return 返回结果
     */
    @RequestMapping("deleteInport")
    public ResultObj deleteInport(Integer id) {
        try {
            inportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
